﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Linq;

namespace park.System_Manager
{
    public partial class AuthorityRoleEdit : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadRoleInfo();
            }
        }

        protected void LoadRoleInfo()
        {
            string id = Request.Params["id"];
            if (!string.IsNullOrEmpty(id))
            {

                cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

              // string json = sl.GetAuthorityByRoleId(id);
                string json = "{\"RoleId\":1,\"RoleName\": \"角色1\",\"AuthorityItems\": \"1,3,4,5,6\"}";


                JObject obj = JObject.Parse(json);
                this.txtRoleName.Text = obj["RoleName"].ToString();
                Response.Write("<script type=\"text/javascript\">var authorityItems = \"" + obj["AuthorityItems"].ToString() + "\";</script>");
            }
            else
            {
                Response.End();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string roleName = this.txtRoleName.Text;
            string idList = this.hdf_SelectedAuthority.Value;
            cn.linkit.epark.ServiceList sl = new cn.linkit.epark.ServiceList();

            string id = Request.Params["id"];
            string name = txtRoleName.Text.Trim();
            string authoritys = this.hdf_SelectedAuthority.Value;

            string jsonString = "{" + string.Format("\"RoleId\":{0},\"RoleName\": \"{1}\",\"AuthorityItems\": \"{2}\"", id, name, authoritys) + "}";

            // bool flag = sl.UpdateRole( jsonString);
            bool flag = true;

            string msg = flag ? "数据保存成功！" : "数据保存失败，请重试...";

            Page.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "<script>alert('" + msg + "');</script>");
        }
    }
}